interface DictState {
  dict: Record<string, any>
}

interface DictItem {
  key: string
  value: any
}

const state: DictState = {
  dict: {},
}

const mutations = {
  SET_DICT: (state: DictState, { key, value }: DictItem) => {
    if (key !== null && key !== '') {
      state.dict[key] = value
    }
  },
  REMOVE_DICT: (state: DictState, key: string) => {
    try {
      if (state.dict.hasOwnProperty(key)) {
        delete state.dict[key]
        return true
      }
    } catch (e) {
      console.error('Error removing dict item:', e)
    }
  },
  CLEAN_DICT: (state: DictState) => {
    state.dict = {}
  },
}

type Commit = <T>(type: string, payload?: T) => void

const actions = {
  // 设置字典
  setDict({ commit }: { commit: Commit }, data: DictItem) {
    commit('SET_DICT', data)
  },
  // 删除字典
  removeDict({ commit }: { commit: Commit }, key: string) {
    commit('REMOVE_DICT', key)
  },
  // 清空字典
  cleanDict({ commit }: { commit: Commit }) {
    commit('CLEAN_DICT')
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
